
package com.insigma.business.qrcode.soldier._bff.impl;


import cn.hutool.core.bean.BeanUtil;

import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.RedisUtils;
import com.insigma.business.qrcode.common.util.RpcContextUtil;
import com.insigma.business.qrcode.common.util.SessionUtils;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.common.vo.SessionSoldier;
import com.insigma.business.qrcode.soldier.dto.PlacementPbNewsDTO;
import com.insigma.business.qrcode.soldier.dto.PlacementPbNewsInsertRequest;
import com.insigma.business.qrcode.soldier.dto.PlacementPbNewsUpdateRequest;
import com.insigma.business.qrcode.soldier.dto.SimpleApiRequest;
import com.insigma.business.qrcode.soldier.dto.gov.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.soldier._bff.PlacementPbNewsBFFProvider;
import com.insigma.business.qrcode.soldier._consumer.PlacementPbNewsConsumer;
import com.insigma.business.qrcode.soldier.service.impl.PlacementPbNewsSearchRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
@Component("placementPbNewsBFFProvider")
public class PlacementPbNewsBFFProviderImpl extends BaseProviderImpl implements PlacementPbNewsBFFProvider {

    @Autowired
    private PlacementPbNewsConsumer placementPbNewsConsumer;

    @Autowired
    private RedisUtils redisUtils;

    private String iLikePrefix = "PLACEMENT_NEWS_I_LIKE_";
    private String dislikePrefix = "PLACEMENT_NEWS_DISLIKE_";


    @Override
    public BasePagingResponse<List<PlacementPbNewsDTO>> searchPlacementPbNews(final PlacementPbNewsSearchRequest searchRequest) {

        final BasePagingResponse<List<PlacementPbNewsDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页PlacementPbNews的请求参数不能为空");
            }

            @Override
            public void executeService() {
                SessionSoldier sessionSoldier = RpcContextUtil.getSessionSoldier();
                if (sessionSoldier != null) {
                    searchRequest.setPublished(true);
                }
                searchRequest.setSortKeyValueJoint("createDatetime_desc");

                BasePagingResponse<List<PlacementPbNewsDTO>> listBasePagingResponse = placementPbNewsConsumer.searchPlacementPbNews(searchRequest);
                BeanUtil.copyProperties(listBasePagingResponse, basePagingResponse);
             }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.PLACEMENT_PB_NEWS_SEARCH;
            }
        });

        return basePagingResponse;
    }



    @Override
    public BaseResponse<PlacementPbNewsDTO> getPlacementPbNews(final BaseGetRequest baseGetRequest){

        final BaseResponse<PlacementPbNewsDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条PlacementPbNews的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final BaseResponse<PlacementPbNewsDTO> placementPbNewsResponse = placementPbNewsConsumer.getPlacementPbNews(baseGetRequest);
                BeanUtil.copyProperties(placementPbNewsResponse, baseResponse);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.PLACEMENT_PB_NEWS_GET;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<String> insertPlacementPbNews(final PlacementPbNewsInsertRequest insertRequest){

        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增PlacementPbNews的请求参数不能为空");
            }
            @Override
            public void executeService() {
                BaseResponse<String> placementPbNewsResponse = placementPbNewsConsumer.insertPlacementPbNews(insertRequest);
                BeanUtil.copyProperties(placementPbNewsResponse, baseResponse);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.PLACEMENT_PB_NEWS_INSERT;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> updatePlacementPbNews(final PlacementPbNewsUpdateRequest updateRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改PlacementPbNews的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
            }
            @Override
            public void executeService() {
                SessionUtils.buildModifierOfGov(updateRequest);
                BaseResponse<Integer> placementPbNewsResponse = placementPbNewsConsumer.updatePlacementPbNews(updateRequest);
                BeanUtil.copyProperties(placementPbNewsResponse, baseResponse);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.PLACEMENT_PB_NEWS_UPDATE;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> deletePlacementPbNews(final BaseDeleteRequest deleteRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除PlacementPbNews的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getId(), "id不能为空");
            }
            @Override
            public void executeService() {
                SessionUtils.buildDeleteOfGov(deleteRequest);
                BaseResponse<Integer> placementPbNewsResponse = placementPbNewsConsumer.deletePlacementPbNews(deleteRequest);
                BeanUtil.copyProperties(placementPbNewsResponse, baseResponse);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.PLACEMENT_PB_NEWS_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<Integer> likeNews(SimpleApiRequest request) {

        BizCheckUtil.checkNotNull(request, "请求不能为空");
        BizCheckUtil.checkNotNull(request.getId(), "新闻id不能为空");
        BizCheckUtil.checkNotNull(request.getLike(), "like不能为空");

        String likeKey = iLikePrefix + request.getId();
        String dislikeKey = dislikePrefix + request.getId();

        if (request.getLike()) {
            redisUtils.sSet(likeKey, SessionUtils.getSoldierUserId());
        } else {
            redisUtils.sSet(dislikeKey, SessionUtils.getSoldierUserId());
        }
        return placementPbNewsConsumer.likeNews(request);
    }

    @Override
    public BaseResponse<Integer> publishNews(PlacementPbNewsUpdateRequest request) {

        BizCheckUtil.checkNotNull(request, "请求不能为空");
        BizCheckUtil.checkNotNull(request.getId(), "新闻id不能为空");
        BizCheckUtil.checkNotNull(request.getPublished(), "状态不能为空");

        return placementPbNewsConsumer.publishNews(request);
    }
}
